package com.tokenplus.thai.checkin.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.tokenplus.thai.R


class CheckinDayAdapter(private val dataList: List<CheckinDay>) :
    RecyclerView.Adapter<CheckinDayAdapter.ViewHolder>() {

    // 当天的坐标
    val location = IntArray(2)
    var itemSize: Int = 0

    //点击事件
    private var mItemClickListener: (CheckinDay) -> Unit = {}

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var rlDay: RelativeLayout =
            view.findViewById(R.id.rlDay)
        var textDay: TextView =
            view.findViewById(R.id.textDay)
        var imageDayDidCheckin: ImageView =
            view.findViewById(R.id.imageDayDidCheckin)
        var imageDayTodayDidCheckin: ImageView =
            view.findViewById(R.id.imageDayTodayDidCheckin)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_checkin_day, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: CheckinDay = dataList[position]

        val textDayParams = holder.textDay.layoutParams
        textDayParams.height = data.size
        textDayParams.width = data.size
        itemSize = data.size
        holder.textDay.setLayoutParams(textDayParams);

        holder.textDay.text = data.day
        holder.imageDayDidCheckin.visibility = View.GONE
        holder.imageDayTodayDidCheckin.visibility = View.GONE

        if (data.isCurrentMonth) {
            holder.textDay.setBackgroundResource(R.drawable.checkin_day_mode2)
            holder.textDay.setTextColor(Color.parseColor("#616161"))

            if (data.isCurrentDay) {
                holder.textDay.setBackgroundResource(R.drawable.checkin_day_mode4)
                holder.textDay.setTextColor(Color.parseColor("#FFFFFF"))

                if (data.isCheckIn) {
                    holder.textDay.setTextColor(Color.parseColor("#002854F6"))
                    holder.imageDayTodayDidCheckin.visibility = View.VISIBLE
                }

                // 获取当前坐标
                holder.textDay.getLocationOnScreen(location)

                val dd = IntArray(2)
                holder.itemView.getLocationOnScreen(dd)

                println("dd:"+dd[0])
                println("dd:"+dd[1])

            } else {
                if (data.isCheckIn) {
                    holder.textDay.setBackgroundResource(R.drawable.checkin_day_mode3)
                    holder.textDay.setTextColor(Color.parseColor("#2854F6"))
                    holder.imageDayDidCheckin.visibility = View.VISIBLE
                }
            }
        } else {
            holder.textDay.setBackgroundResource(R.drawable.checkin_day_mode1)
            holder.textDay.setTextColor(Color.parseColor("#B3B3B3"))
        }

        holder.textDay.setOnClickListener {
            if (data.isCurrentMonth) {
                if (data.isCurrentDay) {
                    if (!data.isCheckIn) {
                        mItemClickListener.invoke(data)
                    }
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    /** 设置点击事件 */
    fun setItemClickListener(listener: (CheckinDay) -> Unit) {
        mItemClickListener = listener
    }
}